package com.ruoyi.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.tws.api.domain.Order;
import com.ruoyi.tws.service.IOrderService;
import com.ruoyi.tws.service.ITwsUseRecordService;
import lombok.RequiredArgsConstructor;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.tws.domain.OrderSecure;
import com.ruoyi.tws.service.IOrderSecureService;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 解除运单申请Controller
 * 
 * @author wjx
 * @date 2023-05-04
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/secure" )
public class OrderSecureController extends BaseController
{
    private final IOrderSecureService iOrderSecureService;
    private final IOrderService orderService;
    private final ITwsUseRecordService useRecordService;

    /**
     * 查询解除运单申请列表
     */
    @PreAuthorize(hasPermi="tws:isecure:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, OrderSecure orderSecure)
    {
        LambdaQueryWrapper<OrderSecure> lqw = new LambdaQueryWrapper<OrderSecure>();
        if (orderSecure.getOrderId() != null) {
            lqw.eq(OrderSecure::getOrderId, orderSecure.getOrderId());
        }
        if (orderSecure.getShippingNoteNumber() != null) {
            lqw.eq(OrderSecure::getShippingNoteNumber, orderSecure.getShippingNoteNumber());
        }
        if (StringUtils.isNotBlank(orderSecure.getDriverName())) {
            lqw.like(OrderSecure::getDriverName ,orderSecure.getDriverName());
        }
        if (StringUtils.isNotBlank(orderSecure.getVehicleNumber())) {
            lqw.eq(OrderSecure::getVehicleNumber, orderSecure.getVehicleNumber());
        }
        if (StringUtils.isNotBlank(orderSecure.getDriverPhone())) {
            lqw.eq(OrderSecure::getDriverPhone, orderSecure.getDriverPhone());
        }
        if (orderSecure.getTotalMonetaryAmount() != null) {
            lqw.eq(OrderSecure::getTotalMonetaryAmount, orderSecure.getTotalMonetaryAmount());
        }
        if (StringUtils.isNotBlank(orderSecure.getSecureReason())) {
            lqw.eq(OrderSecure::getSecureReason, orderSecure.getSecureReason());
        }
        if (StringUtils.isNotBlank(orderSecure.getPlaceOfLoading())) {
            lqw.eq(OrderSecure::getPlaceOfLoading, orderSecure.getPlaceOfLoading());
        }
        if (StringUtils.isNotBlank(orderSecure.getGoodsReceiptPlace())) {
            lqw.eq(OrderSecure::getGoodsReceiptPlace, orderSecure.getGoodsReceiptPlace());
        }
        if (orderSecure.getStatus() != null){
            lqw.eq(OrderSecure::getStatus, orderSecure.getStatus());        }
        lqw.orderByAsc(OrderSecure::getStatus);
        lqw.orderByDesc(OrderSecure::getCreateTime);
        IPage<OrderSecure> list = iOrderSecureService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出解除运单申请列表
     */
    @PreAuthorize(hasPermi = "tws:secure:export")
    @Log(title = "解除运单申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderSecure orderSecure) throws IOException
    {
        LambdaQueryWrapper<OrderSecure> lqw = new LambdaQueryWrapper<OrderSecure>(orderSecure);
        List<OrderSecure> list = iOrderSecureService.list(lqw);
        ExcelUtil<OrderSecure> util = new ExcelUtil<OrderSecure>(OrderSecure. class);
        util.exportExcel(response, list, "secure");
    }

    /**
     * 获取解除运单申请详细信息
     */
    @PreAuthorize(hasPermi="tws:secure:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iOrderSecureService.getById(id));
    }

    /**
     * 新增解除运单申请
     */
    @PreAuthorize(hasPermi="tws:secure:add")
    @Log(title = "解除运单申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderSecure orderSecure) {
        return toAjax(iOrderSecureService.save(orderSecure) ? 1 : 0);
    }

    /**
     * 修改解除运单申请
     */
    @PreAuthorize(hasPermi="tws:secure:edit")
    @Log(title = "解除运单申请" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderSecure orderSecure) {
        return toAjax(iOrderSecureService.updateById(orderSecure) ? 1 : 0);
    }

    /**
     * 删除解除运单申请
     */
    @PreAuthorize(hasPermi="tws:secure:remove")
    @Log(title = "解除运单申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iOrderSecureService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 通过运单申请
     */
    @Transactional
    @PostMapping("/updateSecure")
    public AjaxResult updateSecure(@RequestBody Order order){
        return toAjax(iOrderSecureService.updateSecure(order) ? 1 : 0);
    }

    /**
     * 驳回
     * @param orderSecure
     * @return
     */
    @PostMapping("/rejectSecure")
    @Transactional
    public AjaxResult rejectSecure(@RequestBody OrderSecure orderSecure){
        return toAjax(iOrderSecureService.rejectSecure(orderSecure) ? 1 : 0);
    }


}
